package com.hh.vietnameseproject.ui.read

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
import com.hh.vietnameseproject.R
import com.hh.vietnameseproject.logic.model.ReadList

class ReadItemAdapter(val context:Context?,val myList:ArrayList<ReadList>): RecyclerView.Adapter<ReadItemAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val name = itemView.findViewById<TextView>(R.id.readItemName)
        val url = itemView.findViewById<TextView>(R.id.readItemUrl)
        val layout  =itemView.findViewById<CardView>(R.id.readCardView)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.read_item,parent,false)
        return ViewHolder(view)
    }

    override fun getItemCount() = myList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.name.text = myList[position].name
        holder.url.text = myList[position].url
        holder.layout.setOnClickListener {
            WebActivity.actionStartWebActivity(context,myList[position].url,myList[position].name)
        }
    }


}